home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_bas / tblevw.zip / TABLE.FRM < prev    next >
Text File  |  1994-05-18  |  11KB  |  405 lines

  1. VERSION 2.00
  2. Begin Form TableForm 
  3.    BackColor       =   &H00808080&
  4.    Caption         =   "View Table Sample"
  5.    ClientHeight    =   3150
  6.    ClientLeft      =   1740
  7.    ClientTop       =   2100
  8.    ClientWidth     =   6090
  9.    Height          =   3840
  10.    Icon            =   TABLE.FRX:0000
  11.    Left            =   1680
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   3150
  14.    ScaleWidth      =   6090
  15.    Top             =   1470
  16.    Width           =   6210
  17.    Begin TrueGrid Table1 
  18.       AllowArrows     =   -1  'True
  19.       AllowTabs       =   -1  'True
  20.       BackColor       =   &H00C0C0C0&
  21.       Editable        =   -1  'True
  22.       EditDropDown    =   -1  'True
  23.       ExposeCellMode  =   0  'Expose upon selection
  24.       FetchMode       =   0  'By cell
  25.       FontBold        =   0   'False
  26.       FontItalic      =   0   'False
  27.       FontName        =   "MS Sans Serif"
  28.       FontSize        =   8.25
  29.       FontStrikethru  =   0   'False
  30.       FontUnderline   =   0   'False
  31.       HeadingHeight   =   1
  32.       Height          =   1815
  33.       HorzLines       =   0  'None
  34.       Layout          =   TABLE.FRX:0302
  35.       LayoutIndex     =   1
  36.       Left            =   120
  37.       LinesPerRow     =   1
  38.       MarqueeUnique   =   -1  'True
  39.       SplitPropsGlobal=   -1  'True
  40.       SplitTabMode    =   0  'Don't tab across splits
  41.       TabCapture      =   0   'False
  42.       TabIndex        =   0
  43.       Top             =   120
  44.       UseBookmarks    =   -1  'True
  45.       Width           =   2775
  46.       WrapCellPointer =   0   'False
  47.    End
  48.    Begin Menu ExitMenuOption 
  49.       Caption         =   "E&xit!"
  50.    End
  51.    Begin Menu IndexMenuOption 
  52.       Caption         =   "&Indexes"
  53.       Visible         =   0   'False
  54.       Begin Menu IndexMenu 
  55.          Index           =   0
  56.       End
  57.    End
  58.    Begin Menu HelpMenuOption 
  59.       Caption         =   "&Help"
  60.       Begin Menu HelpMenu 
  61.          Caption         =   "&Index"
  62.          Index           =   0
  63.       End
  64.       Begin Menu HelpMenu 
  65.          Caption         =   "&Using Help"
  66.          Index           =   1
  67.       End
  68.       Begin Menu HelpMenu 
  69.          Caption         =   "-"
  70.          Index           =   2
  71.       End
  72.       Begin Menu HelpMenu 
  73.          Caption         =   "&About View Table..."
  74.          Index           =   3
  75.       End
  76.    End
  77. End
  78.  
  79. Sub CenterForm (F As Form)
  80.  
  81. ' Center the specified form within the screen
  82.  
  83.     F.Move (Screen.Width - F.Width) \ 2, (Screen.Height - F.Height) \ 2
  84.  
  85. End Sub
  86.  
  87. Sub CheckForIndexes ()
  88.  
  89.     ' If Indexes exist then show Index menu option
  90.     If Tb.Indexes.Count > 0 Then
  91.         IndexMenuOption.Visible = True
  92.         IndexMenu(0).Visible = True
  93.         IndexMenu(0).Checked = True
  94.         IndexMenu(0).Caption = "&None"
  95.         
  96.         ' Add Index menu option for each index
  97.         For ct = 0 To Tb.Indexes.Count - 1
  98.             Load IndexMenu(ct + 1)
  99.             IndexMenu(ct + 1).Caption = Tb.Indexes(ct)
  100.             IndexMenu(ct + 1).Checked = False
  101.         Next ct
  102.     End If
  103.  
  104. End Sub
  105.  
  106. Sub ExitApp ()
  107.  
  108.     ' Close database and table before exiting
  109.     Tb.Close
  110.     Db.Close
  111.     End
  112.  
  113. End Sub
  114.  
  115. Sub ExitMenuOption_Click ()
  116.  
  117.     Unload Me
  118.     
  119. End Sub
  120.  
  121. Sub FieldLayout ()
  122.  
  123.     ' Get Field Layout to determine field display
  124.     ' and data entry size
  125.     For ct = 0 To Tb.Fields.Count - 1
  126.         
  127.         'Set display heading to database fieldname
  128.         FldName = Tb.Fields(ct).Name
  129.         Table1.ColumnName(ct + 1) = FldName
  130.         
  131.         'Get width of fieldname
  132.         NameWidth = Len(FldName)
  133.  
  134.         'Get type of field to determine it's display size
  135.         Select Case Tb.Fields(ct).Type
  136.             Case 1, 10      'Text and Logic types
  137.                 FldSize = Tb.Fields(ct).Size
  138.             Case 3          'Integer type
  139.                 FldSize = 7
  140.             Case 4, 8       'Long and date types
  141.                 FldSize = 14
  142.             Case 5, 6, 7    'Currency, Single, Double types
  143.                 FldSize = 10
  144.             Case 11, 12     'Memo and binary types
  145.                 FldSize = 25
  146.         End Select
  147.  
  148.         ' Use field width or the field name width whichever is larger
  149.         If NameWidth > FldSize Then
  150.             Table1.ColumnWidth(ct + 1) = NameWidth + 2
  151.         Else
  152.             Table1.ColumnWidth(ct + 1) = FldSize + 2
  153.         End If
  154.  
  155.         ' Set data entry width to Field size
  156.         Table1.ColumnSize(ct + 1) = FldSize
  157.     Next ct
  158.  
  159. End Sub
  160.  
  161. Sub Form_Load ()
  162.  
  163.     'Center the sample on the screen
  164.     CenterForm TableForm
  165.  
  166.     ' Open Database and Table functions
  167.     OpenDb ("market.mdb")
  168.     OpenTb ("Contact_Info")
  169.     
  170.     ' Estimate begining size, put approx size in MAXROW
  171.     EndRow = MAXROW
  172.     ' Set grid Rows to estimated MAXROW
  173.     Table1.Rows = MAXROW
  174.     ' Set Current Row to one
  175.     Temp = MoveToRow(1)
  176.  
  177.     ' Function to add indexes to the menu if any exist
  178.     CheckForIndexes
  179.  
  180.     ' Function to setup grids columns
  181.     FieldLayout
  182.     
  183. End Sub
  184.  
  185. Sub Form_Resize ()
  186.  
  187.     'Make the grid to the size of the form
  188.     Table1.Move 0, 0, ScaleWidth, ScaleHeight
  189.  
  190. End Sub
  191.  
  192. Sub Form_Unload (Cancel As Integer)
  193.  
  194.     ExitApp
  195.  
  196. End Sub
  197.  
  198. Sub HelpMenu_Click (Index As Integer)
  199.  
  200.     'This event calls the WinHelp EXE and a location to goto based on which selection the user has chosen
  201.     'Case 4 shows the about box for the Callback sample
  202.     Select Case Index
  203.         Case 0
  204.             HelpContext TableForm, HELP_VIEWTABLE
  205.         Case 1
  206.             HelpOnHelp TableForm
  207.         Case 3
  208.             About.Show 1
  209.     End Select
  210.  
  211. End Sub
  212.  
  213. Sub IndexMenu_Click (Index As Integer)
  214.  
  215.   If IndexMenu(Index).Checked <> True Then
  216.   
  217.     ' Set Index to whichever one the user chooses
  218.     Select Case Index
  219.         Case 0
  220.             SetIndex ("")
  221.         Case Else
  222.             SetIndex (IndexMenu(Index).Caption)
  223.     End Select
  224.  
  225.     ' Refresh grid, move to beginning, reset table row
  226.     Table1.Refresh
  227.     Table1.RowIndex = 1
  228.     Temp = MoveToRow(1)
  229.  
  230.     ' Turn off all check marks
  231.     For ct = 0 To Tb.Indexes.Count
  232.         IndexMenu(ct).Checked = False
  233.     Next ct
  234.  
  235.     ' Check value user choose
  236.     IndexMenu(Index).Checked = True
  237.  
  238.   End If
  239.  
  240. End Sub
  241.  
  242. Function MoveToRow (NewRow As Long) As Long
  243.  
  244. Dim CurDiff, EndDiff, BeginDiff As Long
  245.  
  246.     ' Find differences between beginning, end and current position
  247.     CurDiff = Abs(CurrentRow - NewRow)
  248.     EndDiff = EndRow - NewRow
  249.     BeginDiff = NewRow - 1
  250.     
  251.     ' If values are same no need to move db
  252.     If CurrentRow = NewRow Then
  253.         MoveToRow = CurrentRow
  254.         Exit Function
  255.     
  256.     ' If moving forward in db
  257.     ElseIf CurrentRow < NewRow Then
  258.  
  259.         ' Check to see if End is closer, if not
  260.         ' move from current position to new position
  261.         If EndDiff > CurDiff Then
  262.             For ct = 1 To CurDiff
  263.                 Tb.MoveNext
  264.                 If Tb.EOF Then
  265.                     CurrentRow = Tb.RecordCount
  266.                     MoveToRow = CurrentRow
  267.                     Exit Function
  268.                 Else
  269.                     CurrentRow = CurrentRow + 1
  270.                 End If
  271.             Next ct
  272.         
  273.         ' If end is closer move to the end of the database
  274.         ' and go backwards to the new position
  275.         Else
  276.             Tb.MoveLast
  277.             CurrentRow = Tb.RecordCount
  278.             
  279.             'Check to see if estimated equal actual, if not equal
  280.             'exit function so CheckRows can set the actual EndRow value
  281.             If EndRow = Tb.RecordCount Then
  282.                 For ct = 1 To EndDiff
  283.                     Tb.MovePrevious
  284.                     CurrentRow = CurrentRow - 1
  285.                 Next ct
  286.             End If
  287.         End If
  288.     
  289.     ' Moving backward in db
  290.     Else
  291.  
  292.         ' If BeginDiff is greater than CurDiff then move
  293.         ' from current position to new position
  294.         If BeginDiff > CurDiff Then
  295.             For ct = 1 To CurDiff
  296.                 Tb.MovePrevious
  297.                 If Tb.BOF Then
  298.                     CurrentRow = 1
  299.                     MoveToRow = CurrentRow
  300.                     Exit Function
  301.                 Else
  302.                     CurrentRow = CurrentRow - 1
  303.                 End If
  304.             Next ct
  305.         
  306.         ' If beginning is closer then move from
  307.         ' beginning to new position
  308.         Else
  309.             Tb.MoveFirst
  310.             CurrentRow = 1
  311.             For ct = 1 To BeginDiff
  312.                 Tb.MoveNext
  313.                 CurrentRow = CurrentRow + 1
  314.             Next ct
  315.         End If
  316.     End If
  317.     MoveToRow = CurrentRow
  318.  
  319. End Function
  320.  
  321. Sub OpenDb (DbName As String)
  322.  
  323.     ' Put your open database code here
  324.     ChDir App.Path
  325.     Set Db = OpenDatabase(DbName)
  326.  
  327. End Sub
  328.  
  329. Sub OpenTb (TableName As String)
  330.  
  331.     ' Put your open table code here
  332.     Set Tb = Db.OpenTable(TableName)
  333.     
  334. End Sub
  335.  
  336. Sub SetIndex (IndexVal As String)
  337.  
  338.     ' If you database type supports multiple indexes
  339.     ' set the index type you want to use here
  340.     Tb.Index = IndexVal
  341.  
  342. End Sub
  343.  
  344. Sub Table1_CheckRows (RequestRows As Long, CurRows As Long)
  345.  
  346.     ' Move in table to value specified by RequestRows
  347.     NewRow = MoveToRow(RequestRows)
  348.     
  349.     ' If table did not make it to the NewRow value
  350.     ' i.e. NewRow was not attainable then
  351.     ' end of db was reached
  352.     If NewRow <> RequestRows Then
  353.         ' Set CurRows to actual end of file
  354.         CurRows = NewRow
  355.         ' Set EndRow to actual end of file
  356.         EndRow = NewRow
  357.     End If
  358.  
  359. End Sub
  360.  
  361. Sub Table1_Fetch (row As Long, Col As Integer, Value As String)
  362.  
  363.     ' This condition should always be true because of the
  364.     ' code in the CheckRows events but we double check
  365.     NewRow = MoveToRow(row)
  366. '    Debug.Print "OR=" & Str$(row)
  367. '    Debug.Print "NR =" & Str$(NewRow)
  368.  
  369.     If NewRow = row Then
  370.         
  371.         ' If field is empty trap Null and use empty quotes instead
  372.         If IsNull(Tb(Col - 1)) Then
  373.             Value = ""
  374.         Else
  375.             Value = Tb(Col - 1)
  376.         End If
  377.     Else
  378.         MsgBox "Error in navigating database"
  379.     End If
  380.     
  381. End Sub
  382.  
  383. Sub Table1_Update (row As Long, Col As Integer, Value As String)
  384.  
  385.     ' This should always be true because of the code in the
  386.     ' CheckRows but we double check anyways
  387.     If MoveToRow(row) = row Then
  388.         Call UpdateTable(Col, Value)
  389.     Else
  390.         MsgBox "Error updating value"
  391.     End If
  392.  
  393. End Sub
  394.  
  395. Sub UpdateTable (Column As Integer, NewValue As String)
  396.  
  397.         ' There is no error checking so becareful
  398.         ' of data mismatches!!!
  399.         Tb.Edit
  400.         Tb(Column - 1) = NewValue
  401.         Tb.Update
  402.     
  403. End Sub
  404.  
  405.